﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;

public partial class Controls_Club_PostDetail : System.Web.UI.UserControl
{
    private int m_ClubId;
    private int m_Type;
    private int m_ParentId;
    private int m_PostCount;

    protected void AddClubPostPageView(int parentId)
    {
        ClubRules.Instance.AddClubPostPageView(parentId); 
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["clubId"]) && !string.IsNullOrEmpty(Request.QueryString["parentId"]))
        {
            if (m_Type == 1)
            {
                pn_HeaderDetailPage.Visible = false;
                //pn_HeaderMorePage.Visible = true;
                gv_TopicMessageList.AllowPaging = true;
            }
            gv_TopicMessageList.RowDataBound += new GridViewRowEventHandler(gv_TopicMessageList_RowDataBound);
            gv_TopicMessageList.RowCommand += new GridViewCommandEventHandler(gv_TopicMessageList_RowCommand);
            gv_TopicMessageList.PageIndexChanging += new GridViewPageEventHandler(gv_TopicMessageList_PageIndexChanging);
            m_ClubId = Convert.ToInt32(Request.QueryString["clubId"]);
            m_ParentId = Convert.ToInt32(Request.QueryString["parentId"]);
            if (!IsPostBack)
            {
                Page.ClientScript.RegisterClientScriptInclude("FTB-<a href='http://www.freetextbox.com' target=_blank>FreeTextBox</a>", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-<a href='http://www.freetextbox.com' target=_blank>FreeTextBox</a>.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-Utility", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-Utility.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-Toolbars", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-ToolbarItems.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-ImageGallery", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-ImageGallery.js"));
                Page.ClientScript.RegisterClientScriptInclude("FTB-Pro", VirtualPathUtility.MakeRelative(Request.Path, "~/scripts/FTB-Pro.js"));
                InitGrvTopicMessageList();
                AddClubPostPageView(m_ParentId);
                
            }
            //hl_MoreMessage.NavigateUrl = "/Activity/ActivityMessageBoard.aspx?activityId=" + m_ActivityId;
        }

    }

    void gv_TopicMessageList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv_TopicMessageList.PageIndex = e.NewPageIndex;
        InitGrvTopicMessageList();
    }

    void gv_TopicMessageList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName == "Reply")
        //{
        //    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
        //    Panel pnReplyBox = (Panel)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("pn_ReplyBox");
        //    pnReplyBox.Visible = true;
        //    up_MessageList.Update();
        //}
        //if (e.CommandName == "CancelReply")
        //{
        //    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
        //    Panel pnReplyBox = (Panel)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("pn_ReplyBox");
        //    pnReplyBox.Visible = false;
        //    up_MessageList.Update();
        //}
        if (e.CommandName == "DeleteTopic")
        {
            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            Label lbTopicId = (Label)gv_TopicMessageList.Rows[rowIndex].Cells[0].FindControl("lb_TopicId");
            ClubRules.Instance.DeleteClubPostByPostId(Convert.ToInt32(lbTopicId.Text));
            InitGrvTopicMessageList();
            up_MessageList.Update();
        }
        
    }

    void gv_TopicMessageList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbTopicId = (Label)e.Row.Cells[0].FindControl("lb_TopicId");
            Label lbSTUserId = (Label)e.Row.Cells[0].FindControl("lb_STUserId");
            User stuser = UserRules.Instance.GetUserByUserId(lbSTUserId.Text);
            HyperLink hlSTUserPage = (HyperLink)e.Row.Cells[0].FindControl("hl_STUserPage");
            hlSTUserPage.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(lbSTUserId.Text);
            HyperLink hlSTUserName = (HyperLink)e.Row.Cells[0].FindControl("hl_STUserName");
            hlSTUserName.Text = stuser.FullName;
            hlSTUserName.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(lbSTUserId.Text);
            Image imgUserImage = (Image)e.Row.Cells[0].FindControl("img_UserImage");
            imgUserImage.ImageUrl = ApiHelper.Instance.GetUserLogoUrl(lbSTUserId.Text);
            HyperLink hlShortMessage = (HyperLink)e.Row.Cells[0].FindControl("hl_ShortMessage");
            hlShortMessage.Visible = true;
            hlShortMessage.NavigateUrl = "~/Home/SendShortMessage.aspx?uids=" + lbSTUserId.Text;
            Label lbCommentIndex = e.Row.FindControl("lb_CommentIndex") as Label;
            lbCommentIndex.Text = "["+(e.Row.RowIndex+1).ToString()+"]楼";

            if (ClubRules.Instance.GetIsAdminOfClubByUserIdAndClubId(m_ClubId,UserRules.Instance.GetCurrentUserId()))
            {
                LinkButton lbDelete = (LinkButton)e.Row.Cells[0].FindControl("lb_Delete");
                lbDelete.Visible = true;
                lbDelete.OnClientClick = "return confirm('确定要删除这条留言么？')";
            }
        }
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            PlaceHolder phPage = (PlaceHolder)e.Row.Cells[0].FindControl("ph_Page");
        }
    }
    protected void bt_Submit_Click(object sender, EventArgs e)
    {
        ClubRules.Instance.ReplyMessage(UserRules.Instance.GetCurrentUserId(), this.txt_MesasgeWords.Text, m_ClubId, m_ParentId);
        this.txt_MesasgeWords.Text = "";
        InitGrvTopicMessageList();
        up_MessageList.Update();
    }
    
    void bt_Page_Click(object sender, EventArgs e)
    {
        Button btPage = sender as Button;
        gv_TopicMessageList.PageIndex = Convert.ToInt32(btPage.Text) - 1;
        InitGrvTopicMessageList();
    }

    private void InitGrvTopicMessageList()
    {
        m_PostCount = ClubRules.Instance.GetClubPostGroupsByParentIdCount(m_ClubId, m_ParentId);
        gv_TopicMessageList.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        gv_TopicMessageList.DataSource = ClubRules.Instance.GetClubPostGroupsByParentId(m_ClubId,m_ParentId);
        gv_TopicMessageList.DataBind();
    }

    #region Property
    public int ShowType  //0--简化留言板，1--更多留言板
    {
        set
        {
            this.m_Type = value;
        }
    }
    #endregion
}
